extern crate serde_json;

use self::serde_json::{json, Value};
use super::{cur_time, do_cache, do_cache_ret, do_node, do_node_ret};
use base::base::{VBase, VMap};

#[allow(dead_code)]
pub async fn ck_tech(p: String, val: Value) {
    let cmd = vec![
        "all", "year", "te1", "te2", "esop", "bom", "esop_1", "bom_1", "esop_2", "bom_2",
    ];

    let val = val.kobj().unwrap();

    for d in cmd.iter() {
        //println!("===> {d}");

        let m = tech_cmd(d);

        match val.k_val(d) {
            Some(v) => {
                do_node_ret(p.clone(), m.clone(), json!(v)).await;
                do_cache_ret(p.clone(), m, json!(v)).await;
            }
            None => {
                do_node(p.clone(), m.clone()).await;
                do_cache(p.clone(), m).await;
            }
        }
    }
}

#[allow(dead_code)]
pub fn tech_cmd(cmd: &str) -> Value {
    let y = cur_time("%Y");

    let sub = if cmd.starts_with("esop") {
        "esop"
    } else if cmd.starts_with("bom") {
        "bom"
    } else {
        ""
    };

    match cmd {
        "all" => json!({"line": "1", "tp": "tech", "sub": "all"}),
        "year" => json!({"line": "1", "tp": "tech", "sub": "year", "sn": y}),
        "te1" | "te2" => json!({"line": "1", "tp": "tech", "sn": cmd}),
        "esop" | "bom" => json!({"line": "1", "tp": "tech", "sub": sub, "sn": "te1"}),
        "esop_1" | "bom_1" => {
            json!({"line": "1", "tp": "tech", "sub": sub, "sn": "te1", "ext": "M010"})
        }
        "esop_2" | "bom_2" => {
            json!({"line": "1", "tp": "tech", "sub": sub, "sn": "te1", "ext": "M020"})
        }
        _ => panic!("tech_cmd"),
    }
}

#[allow(dead_code)]
pub fn tech_data() -> Value {
    let pstart = cur_time("%Y-%m-%d 00:00:00");
    let y = cur_time("%Y");

    json!({
        "all": [y],
        "year_1": ["te1"],
        "year_2": ["te2"],
        "year": ["te1", "te2"],
        "wsn": ["M010", "M020"],
        "wsn_1": ["M010"],
        "wsn_2": ["M020"],
        "te1": {
            "sn": "te1",
            "name": "name_te1",
            "pstart": pstart,
        },
        "te2": {
            "sn": "te2",
            "name": "name_te2",
            "pstart": pstart,
        },
        "c_te1": {
            "sn": "te1",
            "name": "n_te1",
            "pstart": pstart,
            "task": 1,
        },
        "c_te2": {
            "sn": "te2",
            "name": "n_te2",
            "pstart": pstart,
        },
        "esop": {
            "M010": [
            {"step": 1, "stepcode": 2},
            {"step": 2, "stepcode": 3},
            {"step": 3, "stepcode": 4},
            ],
            "M020": [
            {"step": 1, "stepcode": 5},
            {"step": 2, "stepcode": 6},
            {"step": 3, "stepcode": 7},
            ],
        },
        "esop_c": {
            "M010": [
            {"step": 1, "stepcode": 12},
            {"step": 2, "stepcode": 13},
            {"step": 3, "stepcode": 14},
            ],
            "M020": [
            {"step": 1, "stepcode": 15},
            {"step": 2, "stepcode": 16},
            {"step": 3, "stepcode": 17},
            ],
        },
        "bom": {
            "M010": [
            {"num": 1, "msn": "m1"},
            {"num": 2, "msn": "m2"},
            {"num": 3, "msn": "m3"},
            ],
            "M020": [
            {"num": 1, "msn": "m4"},
            {"num": 2, "msn": "m5"},
            {"num": 3, "msn": "m6"},
            ],
        },
        "bom_c": {
            "M010": [
            {"num": 1, "msn": "m7"},
            {"num": 2, "msn": "m8"},
            {"num": 3, "msn": "m9"},
            ],
            "M020": [
            {"num": 1, "msn": "m10"},
            {"num": 2, "msn": "m11"},
            {"num": 3, "msn": "m12"},
            ],
        }
    })
}
